// run: time $exec < input > output7
#include <iostream>
#include <cstdio>
#include <tr1/unordered_set>
#include <queue>
#include <algorithm>
using namespace std;
using namespace std::tr1;

#define LL long long

#define P pair<int, int>
#define PLI pair<LL, int>
#define X first
#define Y second

const int N = 10;

P a[N];

bool cmp(P a, P b) {
	return a.Y < b.Y;
}


unordered_set<LL> used;

LL ml(int id, LL st, int n) {
	int s = id * n;
	int t = (id + 1) * n;
	int c1 = 0, min_num1 = 10, p1, p2;
	for(int i = t - 1; i >= s; --i) {
		if(st & (1LL << i)) {
			++c1;
			min_num1 = min(min_num1, i - s);
			p1 = i;
		}
	}
	if(c1 == 0) return -1;
	int c2 = 0, min_num2 = 10;
	s -= n;
	t -= n;
	for(int i = t - 1; i >= s; --i) {
		if(st & (1LL << i)) {
			++c2;
			min_num2 = min(min_num2, i - s);
		}
	}
	if(c2 > 0 && min_num1 > min_num2) return -1;
	LL ret = (st ^ (1LL << p1)) | (1LL << (p1 - n));
	return ret;
}

LL mr(int id, LL st, int n) {
	int s = id * n;
	int t = (id + 1) * n;
	int c1 = 0, min_num1 = 10, p1;
	for(int i = t - 1; i >= s; --i) {
		if(st & (1LL << i)) {
			++c1;
			min_num1 = min(min_num1, i - s);
			p1 = i;
		}
	}
	if(c1 == 0) return -1;
	int c2 = 0, min_num2 = 10;
	s += n;
	t += n;
	for(int i = t - 1; i >= s; --i) {
		if(st & (1LL << i)) {
			++c2;
			min_num2 = min(min_num2, i - s);
		}
	}
	if(c2 > 0 && min_num1 > min_num2) return -1;
	LL ret = (st ^ (1LL << p1)) | (1LL << (p1 + n));
	return ret;
}

void print(LL st, int n) {
	for(int i = 0; i < n; ++i) {
		int s = i * n;
		int t = i * n + n;
		for(int j = s; j < t; ++j) {
			if(st & (1LL << j)) cout<<1;
			else cout<<'0';
		}
		cout<<endl;
	}
	cout<<endl;
}

int gao(int n) {
	LL st = 0;

	queue<PLI> que;
	for(int i = 0; i < n; ++i) {
		int p = i * n + a[i].X;
		//cout<<p<<endl;
		st |= (1LL << p);
	}
	//print(st, n);
	que.push(PLI(st, 0));
	used.insert(st);
	LL en = 0;
	for(int i = 0; i < n; ++i) {
		int p = i * n + i;
		en |= (1LL << p);
	}
	//LL tmp = ml(1, st, n);
	//print(tmp, n);
	//cout<<"!"<<endl; print(en, n);
	while(!que.empty()) {
		PLI now = que.front(); que.pop();
		//print(now.X, n);
		//cout<<now.Y<<endl;
		if(now.X == en) return now.Y;
		for(int i = 0; i < n; ++i) {
			if(i == 0) {
				LL tmp = mr(i, now.X, n);
				if(tmp != -1 && used.find(tmp) == used.end()) {
					//print(now.X, n); puts(":"); print(tmp,
					//				  n);
					//cout<<now.Y+1<<endl;
					que.push(PLI(tmp, now.Y + 1));
					used.insert(tmp);
				}
			} else if(i == n - 1) {
				LL tmp = ml(i, now.X, n);
				if(tmp != -1 && used.find(tmp) == used.end()) {
					//print(now.X, n); puts(":"); print(tmp,
					//				  n);

					//cout<<now.Y+1<<endl;
					que.push(PLI(tmp, now.Y + 1));
					used.insert(tmp);
				}
			} else {
				LL tmp = mr(i, now.X, n);
				if(tmp != -1 && used.find(tmp) == used.end()) {
					//print(now.X, n); puts(":"); print(tmp,
					//				  n);

					//cout<<now.Y+1<<endl;
					que.push(PLI(tmp, now.Y + 1));
					used.insert(tmp);
				}
				tmp = ml(i, now.X, n);
				if(tmp != -1 && used.find(tmp) == used.end()) {
					//print(now.X, n); puts(":"); print(tmp,
					//				  n);

					//cout<<now.Y+1<<endl;
					que.push(PLI(tmp, now.Y + 1));
					used.insert(tmp);
				}
			}
		}
	}
	return -1;
}
//100
//010
//001

int main() {
	int T;
	scanf("%d", &T);
	while(T--) {
		int n;
		scanf("%d", &n);
		used.clear();
		for(int i = 0; i < n; ++i) {
			scanf("%d", &a[i].X);
			a[i].Y = i;
		}
		sort(a, a + n);
		for(int i = 0; i < n; ++i) {
			a[i].X = i;
		}
		sort(a, a + n, cmp);
		//for(int i = 0; i < n; ++i) {
		//	cout<<a[i].X<<' ';
		//}
		//cout<<endl;
		cout<<gao(n)<<endl;
	}
	return 0;
}

